//
//  AppDelegate.m
//  ITHome
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "AppDelegate.h"
#import "UMSocial.h"              //友盟第三方分享
#import "UMSocialQQHandler.h" // QQ分享
#import "UMSocialWechatHandler.h" // 微信分享
#import "TabBarViewController.h"
#import "NavigationController.h"
#import "NewfeatureViewController.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import "WeiboSDK.h"

@interface AppDelegate ()<WeiboSDKDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 1.创建窗口
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"] == YES)
    {
        self.window.alpha = 0.4;
    }
    else
    {
        self.window.alpha = 1;
    }
    
    // 2.判断新版本和创建主控制器
    [self newVersionAndRootViewController];
    
    // 3.注册第三方key
    [self registerTheThirdKey];

    // 4.显示窗口
    [self.window makeKeyAndVisible];
    
    
    
    return YES;
}

#pragma mark - 注册第三方key
- (void)registerTheThirdKey
{
    // 友盟第三方分享
    [UMSocialData setAppKey:@"55a8c25567e58e15d8000cb2"];
    
    // QQ分享
    [UMSocialQQHandler setQQWithAppId:@"1104700113" appKey:@"0yknbiDLpnId41QI" url:@"http://www.umeng.com/social"];
    
    // 微信分享
    [UMSocialWechatHandler setWXAppId:@"wx8456ef1f2fa79031" appSecret:@"6c0033485f31bee09ce412418a089243" url:@"http://www.umeng.com/social"];

    
    // 注册微博登陆appKey
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:kWeiboAppKey];
}

#pragma mark - 判断新版本和设置主控制器
- (void)newVersionAndRootViewController
{
    // 创建我的家园对象
    MyHomeViewController *myHoneViewVc = [[MyHomeViewController alloc] init];
    NavigationController *nav = [[NavigationController alloc] initWithRootViewController:myHoneViewVc];
    
    // 创建TabBar对象
    TabBarViewController *tabBarViewVc = [[TabBarViewController alloc] init];
    
    // 设置抽屉
    MMDrawerController *MMDrawerViewVc = [[MMDrawerController alloc] initWithCenterViewController:tabBarViewVc leftDrawerViewController:nav];
    
    [MMDrawerViewVc setOpenDrawerGestureModeMask:MMOpenDrawerGestureModePanningNavigationBar];
    
    [MMDrawerViewVc setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    // 2.设置跟控制器
    // 版本新特性
    NSString *key = @"CFBundleVersion";
    
    // 上一次的使用版本 (存储在沙盒中的版本号)
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults]objectForKey:key];
    
    // 当前软件的版本号（从Info.plist）
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
    
    // 判断版本号是否相同
    if ([currentVersion isEqualToString:lastVersion])
    {
        // 旧版本
        self.window.rootViewController = MMDrawerViewVc;
    }
    else
    {
        // 新版本
        self.window.rootViewController = [[NewfeatureViewController alloc] init];
        //将当前的版本号存进沙盒
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

#pragma mark - 第三方
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    
    if ([TencentOAuth HandleOpenURL:url] == YES)
    {
       return [TencentOAuth HandleOpenURL:url];
    }
    else if ([WeiboSDK handleOpenURL:url delegate:self] == YES)
    {
        return [WeiboSDK handleOpenURL:url delegate:self];
    }
    else
    {
        return [UMSocialSnsService handleOpenURL:url];
    }
}

#pragma mark - 第三方
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    
    if ([TencentOAuth HandleOpenURL:url] == YES)
    {
        return [TencentOAuth HandleOpenURL:url];
    }
    else if ([WeiboSDK handleOpenURL:url delegate:self] == YES)
    {
        return [WeiboSDK handleOpenURL:url delegate:self];
    }
    else
    {
        return [UMSocialSnsService handleOpenURL:url];
    }
}

#pragma mark - 实现微博代理方法
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
    
}

#pragma mark - 授权成功后调用该代理方法
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
    [TheThirdCall theThirdCallShare].wbtoken = [(WBAuthorizeResponse *)response accessToken];
    [TheThirdCall theThirdCallShare].wbCurrentUserID = [(WBAuthorizeResponse *)response userID];
    [TheThirdCall theThirdCallShare].wbRefreshToken = [(WBAuthorizeResponse *)response refreshToken];
    
    // 条用解析方法
    [[TheThirdCall theThirdCallShare] getSinaUserInfo];
    
    // 创建一个通知·· 用户登陆状态的通知,让相关页面刷新数据
    [[NSNotificationCenter defaultCenter] postNotificationName:kSinaLoginSuccessed object:self];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
